package com.qingxin.wuye.repertory.service;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.repertory.entity.RukuDocket;

@Service
public class RukuDocketService extends BaseService<RukuDocket, String>{
	
	public Pager list(String searchname, String repertoryid,PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.wuye.repertory.entity.RukuDocket(" +
				"r.docketid, r.repertoryid, r.dockeCode, r.happenTime, r.jingshou, r.caigou, r.yanhuo, r.yanshou,r.remark,t.name as repertoryName)" +
				" from RukuDocket r, Repertory t" +
				" where r.repertoryid = t.repertoryid ");
		/*if(StringUtils.isNotEmpty(searchname)){
			searchname = searchname.replace("'", "");
			querier.append(" and name like :name ");
			querier.setParam("name", "%"+searchname+"%");
		}*/
		
		if(StringUtils.isNotEmpty(repertoryid)){
			querier.append(" and r.repertoryid = :repertoryid ");
			querier.setParam("repertoryid", repertoryid);
		}
		
		querier.append(pageParam.getOrderby(" order by r.createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
		
	}
	
	
	public SysError add(RukuDocket rukuDocket, LoginUser loginUser, HttpServletRequest request){
		rukuDocket.setCreateTime(new Date());
		rukuDocket.setCreateBy(loginUser.getLogin_name());
		this.save(rukuDocket);
		return null;
	}
	
	public SysError update(RukuDocket rukuDocket, LoginUser loginUser, HttpServletRequest request){
		RukuDocket old = getSafeEntity(rukuDocket.getDocketid());
		String fields = "repertoryid,dockeCode,happenTime,jingshou,caigou,yanhuo,yanshou,remark";
		BeanUtil.setTargetFields(old, rukuDocket, fields, true);
		Updater<RukuDocket> updater = new Updater<>(old, fields);
		this.update(updater);
		return null;
	}
	
	
	public Object delete(String[] docketid, LoginUser loginUser, HttpServletRequest request){
		for(String id : docketid){
			RukuDocket rukuDocket = get(id);
			if(rukuDocket==null){
				continue;
			}
			this.delete(rukuDocket);
		}
		
		return null;
	}
}
